<?php

namespace teach\controllers;

use teach\components\WeChatPay;
use teach\service\Pay;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

class PayController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 直接购买
     */
    public function actionDirect()
    {
        //获取配置
//        $appId = Yii::$app->params['mini']['wxAppId'];
//        $mchId = Yii::$app->params['mini']['mchId'];
//        $key = Yii::$app->params['mini']['md5Key'];
//        $notify_url = Yii::$app->params['mini']['notifyUrlDirect'];
        $spdill_create_ip = $_SERVER['REMOTE_ADDR'];

        //获取产品

        $request = Yii::$app->request;

        $name = $request->post('name');//收货人
        $phone = $request->post('phone');//电话
        $address = $request->post('address');//地址

        $mid = $request->post('mid');//商家id
        $pid = $request->post('pid');//产品id
        $uid = $request->post('uid');//用户id不能为空
        $sku_id = $request->post('sku_id');//sku表id
        $count = $request->post('count');//购买的数量
        $openid = $request->post('openid'); //openid
        $value = $request->post('value'); //购买的属性值

        //判断商户是否存在
        if (!array_key_exists($mid, Yii::$app->params['serviceNum'])) {
            $jsonData = ResponseHelper::buildJsonData(400, '商家支付账号不存在,请联系商家或者开发者');
            return $this->asJson($jsonData);
        }
        //服务商模式下支付
        $appid = Yii::$app->params['serviceNum'][$mid]['appid']; //公众号appid
        $mch_id = Yii::$app->params['serviceNum']['mchId']; //服务商号
        $sub_mch_id = Yii::$app->params['serviceNum'][$mid]['sub_mch_id'];
        $sub_appid = Yii::$app->params['serviceNum'][$mid]['sub_appid']; //小程序appid
        $key = Yii::$app->params['serviceNum']['md5Key']; //支付密匙设置
        $notify_url = Yii::$app->params['serviceNum'][$mid]['notifyUrlDirect']; //支付回调


        $msg = '';
        $flag = false;
        if (!$openid) {
            $msg = 'openid 用户openid不能为空';
        } elseif (!$mid) {
            $msg = 'mid 商家id不能为空';
        } elseif (!$pid) {
            $msg = 'pid 产品id不能为空';
        } elseif (!$uid) {
            $msg = 'uid 用户id不能为空';
        } elseif (!$sku_id) {
            $msg = 'sku_id 不能为空';
        } elseif (!$count) {
            $msg = 'count 购买数量不能为空';
        } elseif (!$value) {
            $msg = 'value 购买规格不能为空';
        } elseif (!$name) {
            $msg = 'name 收货人不能为空';
        } elseif (!$phone) {
            $msg = 'phone 电话不能为空';
        } elseif (!$address) {
            $msg = 'address 地址不能为空';
        } elseif (!$appid) {
            $msg = 'appid 不能为空';
        } elseif (!$mch_id) {
            $msg = 'mchid 不能为空';
        } elseif (!$key) {
            $msg = 'key 不能为空';
        } elseif (!$sub_appid) {
            $msg = 'sub_appid不能为空';
        } elseif (!$notify_url) {
            $msg = 'url 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $pays = new Pay();

            //TODO 1获取mall_distribute
            $distribution = $pays->distribution($mid);
            if (!$distribution) {
                $jsonData = ResponseHelper::buildJsonData(400, '商家的折扣价规则不存在,请联系商家');
                return $this->asJson($jsonData);
            }
            //判断商品是否存在
            $parameter = $sku_id;
            $param = $pid;
            $product = $pays->productDetail($parameter, $param);
            if (!$product) {
                $jsonData = ResponseHelper::buildJsonData(400, '商品不存在,请核实后再下单');
                return $this->asJson($jsonData);
            }

            //判断是否修改了sku的属性值
            if ($product['s_status'] == -1) {
                $jsonData = ResponseHelper::buildJsonData(400, '商品发生了改变,请删除了重新下单');
                return $this->asJson($jsonData);
            }


            //判断商品是否已经下架
            if ($product['is_up'] != 1) {
                $jsonData = ResponseHelper::buildJsonData(400, '商品已经下架,请购买其他商品');
                return $this->asJson($jsonData);
            }

            //判断商品的库存是否还有
            if ($product['stock'] < $count) {
                $jsonData = ResponseHelper::buildJsonData(400, '库存不足');
                return $this->asJson($jsonData);
            }

            // 查找出产品的折扣价
//            $where = " state = 1 ";
//            $discount = $pays->productDiscount($where);
            $out_trade_no = 'No'. date('Ymd') . time() . str_pad(mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
            $total_fee = $product['price'] * $count * $distribution['discount'];
            $logistic_fee = $product['freight'];
            if ($total_fee < $distribution['low_total_fee'] && $uid != 14017) { //低于50不包邮
                if ($logistic_fee == 0 ) {
                    $total_fee = $total_fee + $distribution['logistic_fee']; //加上12元邮费
                    $logistic_fee = $distribution['logistic_fee'];
                } else {
                    $total_fee = $total_fee + $product['freight'];
                    $logistic_fee = $product['freight'];
                }
            }
            if ($total_fee < 0.01) {
                $total_fee = 0.01;
            }
            $body = '直接购买产品';

            //拿取充值的金额
            $recharge = $request->post('recharge', 0);

            //如果充值金额为0 则直接走微信购买
            if ($recharge != 0) {
                //查找用户看看是否有足够的充值金额
                $user = $pays->userOne($uid);
                if ($recharge > $user['recharge']) {
                    $jsonData = ResponseHelper::buildJsonData(401, '输入的支票不对');
                    return $this->asJson($jsonData);
                }

                //判断支票输入是否大于 要支付的额度
                if ($recharge > $total_fee) {
                    $jsonData = ResponseHelper::buildJsonData(401, '支票额度有误');
                    return $this->asJson($jsonData);
                }
            }
            //如果充值金额不为0, 判断商品的金额跟传递的金额是否相等 相等则直接扣积分，不相等，在结算金额后相应扣去充值金额跟拉取微信支付
            if ($recharge == $total_fee) {

                //TODO 减去用户充值的金额
                $recharge_res = $pays->userSubRecharge($uid, $recharge);
                if ($recharge_res) {
                    $result['code'] = 1;
                    $result['pay_way'] = 2; //2表示全部用支票金额支付
                    $status = 1;
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '用户支票减去失败1');
                    return $this->asJson($jsonData);
                }
            } else {
                //计算一下要支付的金额
                $total_fee = (float)number_format($total_fee - $recharge, 2, '.', '');

                //实例化支付
                $WeiXinPay = new WeChatPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $notify_url, $spdill_create_ip, $sub_appid, $sub_mch_id);
                //发起微信支付
                $result = $WeiXinPay->pay();
                $result['pay_way'] = 1; //1表示用 微信支付或者 微信跟支票混用
                $status = -1;


            }

            if ($result['code'] == 0) {
                $jsonData = ResponseHelper::buildJsonData(400, '失败', $result['msg']);
                return $this->asJson($jsonData);
            } else {

                //生成预支付成功 对产品进行入库记录
                $order = [
                    'mid' => $mid,
                    'uid' => $uid,
                    'openid' => $openid,
                    'out_trade_no' => $out_trade_no,
                    'total_fee' => $total_fee,
                    'amount' => $product['price'] * $count,
                    'logistic_fee' => $logistic_fee,
                    'name' => $name,
                    'phone' => $phone,
                    'address' => $address,
                    'create_time' => time(),
                    'status' => $status,
                    'marks' => $request->post('marks'),
                    'mode' => 1,
                    'recharge' => $recharge,
                    'discount' => $distribution['discount'],
                ];
                $orderList = [
                    'mid' => $mid,
                    'pid' => $pid,
                    'out_trade_no' => $out_trade_no,
                    'logistic' => $logistic_fee,
                    'price' => $product['price'],
                    'value' => $value,
                    'count' => $count,
                    'create_time' => time(),
                    'total_fee' => $total_fee,
                    'uid' => $uid,
                    'sku_id' => $sku_id,
                    'amount' => $product['price'] * $count,
                    'level_one' => $product['level_one'],
                    'level_two' => $product['level_two'],
                    'product_no' => $product['product_no']
                ];

                $res = $pays->orderInsert($order, $orderList);

                if ($res) {
                    //获取订单id
                    $orderId = $pays->orderGetId($out_trade_no);
                    $result['oid'] = $orderId['id'];
                    //减去相应的库存
                    $pays->productReduce($sku_id, $count);
                    $jsonData = ResponseHelper::buildJsonData(200, '成功', $result);
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '订单生成失败,请联系管理员');
                }

            }

        }
        return $this->asJson($jsonData);

    }

    /**
     * 购物车里购买
     */
    public function actionCart()
    {
        //获取配置
//        $appId = Yii::$app->params['mini']['wxAppId'];
//        $mchId = Yii::$app->params['mini']['mchId'];
//        $key = Yii::$app->params['mini']['md5Key'];
//        $notify_url = Yii::$app->params['mini']['notifyUrlDirect'];
        $spdill_create_ip = $_SERVER['REMOTE_ADDR'];

        //获取产品

        $request = Yii::$app->request;

        $name = $request->post('name');//收货人
        $phone = $request->post('phone');//电话
        $address = $request->post('address');//地址

        $mid = $request->post('mid');//商家id
        $uid = $request->post('uid');//用户id不能为空
        $openid = $request->post('openid'); //openid

        //判断商户是否存在
        if (!array_key_exists($mid, Yii::$app->params['serviceNum'])) {
            $jsonData = ResponseHelper::buildJsonData(400, '商家支付账号不存在,请联系商家或者开发者');
            return $this->asJson($jsonData);
        }

        //服务商模式下支付
        $appid = Yii::$app->params['serviceNum'][$mid]['appid']; //公众号appid
        $mch_id = Yii::$app->params['serviceNum']['mchId']; //服务商号
        $sub_mch_id = Yii::$app->params['serviceNum'][$mid]['sub_mch_id'];
        $sub_appid = Yii::$app->params['serviceNum'][$mid]['sub_appid']; //小程序appid
        $key = Yii::$app->params['serviceNum']['md5Key']; //支付密匙设置
        $notify_url = Yii::$app->params['serviceNum'][$mid]['notifyUrlDirect']; //支付回调


//        $pid = $request->post('pid');//产品id
//        $count= $request->post('count');//购买的数量
//        $sku_id = $request->post('sku_id');//sku表id

        $carts = $request->post('carts'); //产品json数据集

        //参数是否齐全
        $msg = '';
        $flag = false;
        if (!$openid) {
            $msg = 'openid 用户openid不能为空';
        } elseif (!$mid) {
            $msg = 'mid 商家id不能为空';
        } elseif (!$uid) {
            $msg = 'uid 用户id不能为空';
        } elseif (!$name) {
            $msg = 'name 收货人不能为空';
        } elseif (!$phone) {
            $msg = 'phone 电话不能为空';
        } elseif (!$address) {
            $msg = 'address 地址不能为空';
        } elseif (!$carts) {
            $msg = '购物车产品不能为空';
        } elseif (!$appid) {
            $msg = 'appid 不能为空';
        } elseif (!$mch_id) {
            $msg = 'mchid 不能为空';
        } elseif (!$key) {
            $msg = 'key 不能为空';
        } elseif (!$notify_url) {
            $msg = 'url 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $pays = new Pay();

            //查找出产品的折扣价
            //$where = " state = 1 ";
            //$discount = $pays->productDiscount($where);
            $distribution = $pays->distribution($mid);
            if (!$distribution) {
                $jsonData = ResponseHelper::buildJsonData(400, '商家的折扣价规则不存在,请联系商家');
                return $this->asJson($jsonData);
            }


            //定义快递数组费用
            $freightArr = [];
            $total_fee_count = 0;
            $orderList = [];
            //处理购物车产品集
            $pds = json_decode($carts, true);
            foreach ($pds as $p_key => $val) {
                //看看每个产品的需要的key存在不存在
                if (!array_key_exists('pid', $val)) {
                    $jsonData = ResponseHelper::buildJsonData(400, '缺少产品key值:pid');
                    return $this->asJson($jsonData);
                }
                if (!array_key_exists('count', $val)) {
                    $jsonData = ResponseHelper::buildJsonData(400, '缺少产品key值:count');
                    return $this->asJson($jsonData);
                }
                if (!array_key_exists('sku_id', $val)) {
                    $jsonData = ResponseHelper::buildJsonData(400, '缺少产品key值:sku_id');
                    return $this->asJson($jsonData);
                }
                if (!array_key_exists('value', $val)) {
                    $jsonData = ResponseHelper::buildJsonData(400, '缺少产品key值:value');
                    return $this->asJson($jsonData);
                }
            }

            $out_trade_no = 'No' . date('Ymd') . time() . str_pad(mt_rand(1, 999), 3, '0', STR_PAD_LEFT);

            //循环查找每个产品,放入到动态变量中数组中
            foreach ($pds as $_key => $_val) {
                $parameter = $_val['sku_id'];
                $param = $_val['pid'];
                $product = $pays->productDetail($parameter, $param);
                //查看每个产品是否存在
                if (!$product) {
                    $jsonData = ResponseHelper::buildJsonData(400, '商品不存在,请核实后再下单');
                    return $this->asJson($jsonData);
                }

                //判断是否修改了sku的属性值
                if ($product['s_status'] == -1) {
                    $jsonData = ResponseHelper::buildJsonData(400, '商品发生了改变,请删除了重新下单');
                    return $this->asJson($jsonData);
                }

                //查看每个产品是否下架
                if ($product['is_up'] != 1) {
                    $jsonData = ResponseHelper::buildJsonData(400, '商品已经下架,请购买其他商品');
                    return $this->asJson($jsonData);
                }
                //判断每个产品库存是否足够
                if ($product['stock'] < $_val['count']) {
                    $jsonData = ResponseHelper::buildJsonData(400, '库存不足');
                    return $this->asJson($jsonData);
                }

                $freightArr[$_key] = $product['freight'];
                $total_fee_count = $total_fee_count + $product['price'] * $_val['count'];


                //需要入库的订单产品列表
                $orderList [] = [
                    'mid' => $mid,
                    'pid' => $product['id'],
                    'out_trade_no' => $out_trade_no,
                    'logistic' => $product['freight'],
                    'price' => $product['price'],
                    'count' => $_val['count'],
                    'create_time' => time(),
                    'total_fee' => $product['price'] * $distribution['discount'] * $_val['count'],
                    'uid' => $uid,
                    'value' => $_val['value'],
                    'sku_id' => $_val['sku_id'],
                    'amount' => $product['price'] * $_val['count'],
                    'level_one' => $product['level_one'],
                    'level_two' => $product['level_two'],
                    'product_no' => $product['product_no']
                ];
            }

            $freight = max($freightArr);
            $total_fee = $total_fee_count * $distribution['discount'];
            if ($total_fee < $distribution['low_total_fee'] && $uid != 10014) {
                if ($freight == 0) {
                    $total_fee = $total_fee + $distribution['logistic_fee'];
                    $freight = $distribution['logistic_fee'];
                } else {
                    $total_fee = $total_fee + $freight;
                }
            }
            if ($total_fee < 0.01) {
                $total_fee = 0.01;
            }
            $body = '购物车支付';
            //拿取充值的金额
            $recharge = $request->post('recharge', 0);

            //如果充值金额为0 则直接走微信购买
            if ($recharge != 0) {
                //查找用户看看是否有足够的充值金额
                $user = $pays->userOne($uid);
                if ($recharge > $user['recharge']) {
                    $jsonData = ResponseHelper::buildJsonData(401, '输入的支票不对');
                    return $this->asJson($jsonData);
                }

                //判断支票输入是否大于 要支付的额度
                if ($recharge > $total_fee) {
                    $jsonData = ResponseHelper::buildJsonData(401, '支票额度有误');
                    return $this->asJson($jsonData);
                }
            }

            if ($recharge == $total_fee) {

                //减去用户充值的金额
                $recharge_res = $pays->userSubRecharge($uid, $recharge);
                if ($recharge_res) {
                    $result['code'] = 1;
                    $result['pay_way'] = 2; //2表示全部用支票支付
                    $status = 1;
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '用户支票减去失败1');
                    return $this->asJson($jsonData);
                }

            } else {

                //计算一下要支付的金额
                $total_fee = (float)number_format($total_fee - $recharge, 2, '.', '');


                //实例化支付
                $WeiXinPay = new WeChatPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $notify_url, $spdill_create_ip, $sub_appid, $sub_mch_id);
                //发起微信支付
                $result = $WeiXinPay->pay();
                $result['pay_way'] = 1; //用户微信支付 或者微信跟支票混用
                $status = -1;


            }

            if ($result['code'] == 0) {
                $jsonData = ResponseHelper::buildJsonData(400, '失败', $result['msg']);
                return $this->asJson($jsonData);
            } else {
                //生成预支付成功 对产品进行入库记录
                $order = [
                    'mid' => $mid,
                    'uid' => $uid,
                    'openid' => $openid,
                    'out_trade_no' => $out_trade_no,
                    'total_fee' => $total_fee,
                    'amount' => $total_fee_count,
                    'logistic_fee' => $freight,
                    'name' => $name,
                    'phone' => $phone,
                    'address' => $address,
                    'create_time' => time(),
                    'status' => $status,
                    'marks' => $request->post('marks'),
                    'mode' => 2,
                    'recharge' => $recharge,
                    'discount' => $distribution['discount'],
                ];

                $res = $pays->orderCartInsert($order, $orderList);

                if ($res) {
                    //获取订单id
                    $orderId = $pays->orderGetId($out_trade_no);
                    $result['oid'] = $orderId['id'];
                    //TODO 减去库存
                    foreach ($orderList as $boy => $girl) {
                        $pays->productReduce($girl['sku_id'], $girl['count']);
                    }
                    $jsonData = ResponseHelper::buildJsonData(200, '成功', $result);
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '订单生成失败,请联系管理员');
                }
            }
        }

        return $this->asJson($jsonData);

    }

    /**
     * 重新支付
     * @return \yii\web\Response
     */
    public function actionAgain()
    {
        //获取配置
//        $appId = Yii::$app->params['mini']['wxAppId'];
//        $mchId = Yii::$app->params['mini']['mchId'];
//        $key = Yii::$app->params['mini']['md5Key'];
//        $notify_url = Yii::$app->params['mini']['notifyUrlDirect'];
        $spdill_create_ip = $_SERVER['REMOTE_ADDR'];

        //获取参数
        $request = Yii::$app->request;

        $oid = $request->post('oid'); //订单id
        $uid = $request->post('uid'); //用户uid
        $mid = $request->post('mid'); //用户mid
        $openid = $request->post('openid'); //用户openid

        //判断商户是否存在
        if (!array_key_exists($mid, Yii::$app->params['serviceNum'])) {
            $jsonData = ResponseHelper::buildJsonData(400, '商家支付账号不存在,请联系商家或者开发者');
            return $this->asJson($jsonData);
        }

        //服务商模式下
        $appid = Yii::$app->params['serviceNum'][$mid]['appid']; //公众号appid
        $mch_id = Yii::$app->params['serviceNum']['mchId']; //服务商号
        $sub_mch_id = Yii::$app->params['serviceNum'][$mid]['sub_mch_id'];
        $sub_appid = Yii::$app->params['serviceNum'][$mid]['sub_appid']; //小程序appid
        $key = Yii::$app->params['serviceNum']['md5Key']; //支付密匙设置
        $notify_url = Yii::$app->params['serviceNum'][$mid]['notifyUrlDirect']; //支付回调

        $msg = '';
        $flag = false;
        if (!$openid) {
            $msg = 'openid 不能为空';
        } elseif (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$appid) {
            $msg = 'appid 不能为空';
        } elseif (!$mch_id) {
            $msg = 'mchid 不能为空';
        } elseif (!$key) {
            $msg = 'key 不能为空';
        } elseif (!$notify_url) {
            $msg = 'url 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $pays = new Pay();

            //查询总订单记录
            $map = " id = {$oid} AND uid = {$uid} AND mid = {$mid} ";
            $orderTotal = $pays->orderOne($map);
            if (!$orderTotal) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单有误,请核实后重新下单');
                return $this->asJson($jsonData);
            }

            if ($orderTotal["status"] == -4) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单有误,请删除后重新下单');
                return $this->asJson($jsonData);
            }

            //查出分订单记录
            $mapper = " mid = {$mid} AND out_trade_no = '{$orderTotal['out_trade_no']}' ";
            $orderList = $pays->orderList($mapper);

            if (!$orderList) {
                // 将status 改为 -4 订单失效
                $pays->orderUnusual($map);
                $jsonData = ResponseHelper::buildJsonData(400, '订单列表有误,请重新下单');
                return $this->asJson($jsonData);
            }

            //定义变量
            $freightArr = [];
            $orderListId = [];
            $total_fee_count = 0;

            //查看产品是否存在 是否还上架 是否正常 库存是否充足 有异常的情况下 将status 改为-2
            foreach ($orderList as $_key => $value) {
                //查询每个产品 进行比对
                $product = $pays->productDetails($value['pid']);
                //查看每个产品是否存在
                if (!$product) {
                    // 将status 改为 -4 订单失效
                    $pays->orderUnusual($map);
                    $jsonData = ResponseHelper::buildJsonData(400, '部分商品不存在,请核实后再下单');
                    return $this->asJson($jsonData);
                }
                //查看每个产品是否下架
                if ($product['is_up'] != 1) {
                    // 将status 改为 -4
                    $pays->orderUnusual($map);
                    foreach ($orderList as $boy => $girl) {
                        $pays->productIncrease($girl['sku_id'], $girl['count']);
                    }
                    $jsonData = ResponseHelper::buildJsonData(400, '部分商品已经下架,请重新下单');
                    return $this->asJson($jsonData);
                }
                //判断每个产品库存是否足够
                if ($product['stock'] < $value['count']) {
                    // 将status 改为 -4
                    $pays->orderUnusual($map);
                    //库存加回去
                    foreach ($orderList as $boy => $girl) {
                        $pays->productIncrease($girl['sku_id'], $girl['count']);
                    }
                    $jsonData = ResponseHelper::buildJsonData(400, '部分商品库存不足,请重新下载');
                    return $this->asJson($jsonData);
                }

                //重新计算价格
                $freightArr[$_key] = $product['freight'];
                $orderListId[] = $value['id'];
                $total_fee_count = $total_fee_count + $product['price'] * $value['count'];
            }

            //查找出产品的折扣价
            $distribution = $pays->distribution($mid);
            if (!$distribution) {
                $jsonData = ResponseHelper::buildJsonData(400, '商家的折扣价规则不存在,请联系商家');
                return $this->asJson($jsonData);
            }

            $freight = max($freightArr);
            $total_fee = $total_fee_count * $distribution['discount'] ;
            if ($total_fee < $distribution['low_total_fee']) {
                if ($freight == 0) {
                    $total_fee = $total_fee + $distribution['logistic_fee'];
                } else {
                    $total_fee = $total_fee + $distribution['logistic_fee'];
                }
            }

            if ($total_fee < 0.01) {
                $total_fee = 0.01;
            }
            //比对原先价格是否一样, 不一样则返回错误
            if ($orderTotal['total_fee'] != $total_fee) {
                // 将status 改为 -4
                $pays->orderUnusual($map);
                //库存加回去
                foreach ($orderList as $boy => $girl) {
                    $pays->productIncrease($girl['sku_id'], $girl['count']);
                }
                $jsonData = ResponseHelper::buildJsonData(400, '价格发生了改变,请重新下单');
                return $this->asJson($jsonData);
            }

            //如果一样则发起支付
            $out_trade_no = date('Ymd') . time() . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            $body = '重新发起支付';

            //实例化支付
            $WeiXinPay = new WeChatPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $notify_url, $spdill_create_ip, $sub_appid, $sub_mch_id);
            //发起微信支付
            $result = $WeiXinPay->pay();

            if ($result['code'] == 0) {
                $jsonData = ResponseHelper::buildJsonData(400, '失败', $result['msg']);
                return $this->asJson($jsonData);
            } else {
                //更新订单信息
                $whereList = join(',', $orderListId);
                $param = " out_trade_no = '{$out_trade_no}' , mode = 3 ";
                $parameter = " out_trade_no = '{$out_trade_no}' ";
                $res = $pays->orderUpdate($param, $parameter, $map, $whereList);

                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '成功', $result);
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '重新支付失败,请重新下单');
                }
            }
        }
        return $this->asJson($jsonData);
    }
}